package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        // LocalDate to = LocalDate.now();
        // LocalDateTime begin = LocalDateTime.of(to, LocalTime.MIN);
        // LocalDateTime end = LocalDateTime.of(to, LocalTime.MAX);
        Integer validCount = orderMapper.validCountByDate(begin, end);
        Double turnover = orderMapper.getturnover(begin, end);
        Integer newUsers = userMapper.countNewUserByDate(begin,end);
        Integer allorder = orderMapper.getAllorder(begin, end);
        Double unitPrice = allorder==0?0:turnover/allorder;
        Double orderCompletionRate = allorder==0?0:validCount*1.0/allorder;
        validCount = validCount==null?0:validCount;
        turnover = turnover==null?0:turnover;
        newUsers = newUsers==null?0:newUsers;
        // allorder = allorder==null?0:allorder;
        unitPrice = unitPrice==null?0:unitPrice;
        return BusinessDataVO.builder()
                .validOrderCount(validCount)
                .turnover(turnover)
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer valid =setmealMapper.countByStatus(1);
        Integer disvalid = setmealMapper.countByStatus(0);
        return SetmealOverViewVO.builder()
                .sold( valid)
                .discontinued(disvalid)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewDishes() {
        Integer valid =dishMapper.countByStatus(1);
        Integer disvalid = dishMapper.countByStatus(0);
        return SetmealOverViewVO.builder()
                .sold( valid)
                .discontinued(disvalid)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        Integer allOrders= orderMapper.countOrderByStatus(null);
        Integer cancelledOrders = orderMapper.countOrderByStatus(Orders.CANCELLED);
        Integer completedOrders = orderMapper.countOrderByStatus(Orders.COMPLETED);
        Integer deliveredOrders = orderMapper.countOrderByStatus(Orders.DELIVERY_IN_PROGRESS);
        Integer waitingOrders = orderMapper.countOrderByStatus(Orders.TO_BE_CONFIRMED);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

}
